#ifndef	_USTC_USB_CHAP9_H_
#define	_USTC_USB_CHAP9_H_

//#include "comdef.h"

/*USB PROTOCOL CONSTS*/

#define USB_DEVICE_DESCRIPTOR_TYPE                	0x01
#define USB_CONFIGURATION_DESCRIPTOR_TYPE	0x02
#define USB_STRING_DESCRIPTOR_TYPE                	0x03
#define USB_INTERFACE_DESCRIPTOR_TYPE             	0x04
#define USB_ENDPOINT_DESCRIPTOR_TYPE             	0x05
#define USB_POWER_DESCRIPTOR_TYPE                 	0x06

#define MAX_ENDPOINTS      						0x3

#define NUM_ENDPOINTS							2
#define CONFIG_DESCRIPTOR_LENGTH    (sizeof(USB_CONFIGURATION_DESCRIPTOR_T) + sizeof(USB_INTERFACE_DESCRIPTOR_T) + (NUM_ENDPOINTS * sizeof(USB_ENDPOINT_DESCRIPTOR_T)))

/*Values for bmAttributes field of an endpoint descriptor*/

#define USB_ENDPOINT_TYPE_CONTROL                 	0x00
#define USB_ENDPOINT_TYPE_ISOCHRONOUS            0x01
#define USB_ENDPOINT_TYPE_BULK                    		0x02
#define USB_ENDPOINT_TYPE_INTERRUPT              	0x03

#define USB_CLASS_CODE_MASSSTORAGE_CLASS_DEVICE	0x08

#define USB_SUBCLASS_CODE_RBC				0x01
#define USB_SUBCLASS_CODE_SFF8020I		0x02
#define USB_SUBCLASS_CODE_QIC157			0x03
#define USB_SUBCLASS_CODE_UFI			       0x04
#define USB_SUBCLASS_CODE_SFF8070I		0x05
#define USB_SUBCLASS_CODE_SCSI			0x06

#define USB_PROTOCOL_CODE_CBI0			0x00
#define USB_PROTOCOL_CODE_CBI1			0x01
#define USB_PROTOCOL_CODE_BULK			0x50

#define MAXIMUM_USB_STRING_LENGTH 		255

#define STR_INDEX_LANGUAGE				0x00	
#define STR_INDEX_MANUFACTURER			0x01	
#define STR_INDEX_PRODUCT					0x02	
#define STR_INDEX_SERIALNUMBER			0x03
#define STR_INDEX_CONFIGURATION			0x04	
#define STR_INDEX_INTERFACE				0x05

/*mask*/

#define USB_ENDPOINT_DIRECTION_MASK               0x80

#define USB_RECIPIENT         		(USTC_U8)0x1f
#define USB_RECIPIENT_DEVICE    	(USTC_U8)0x00
#define USB_RECIPIENT_INTERFACE  	(USTC_U8)0x01
#define USB_RECIPIENT_ENDPOINT   	(USTC_U8)0x02

#define USB_REQUEST_TYPE_MASK    	(USTC_U8)0x60
#define USB_STANDARD_REQUEST     	(USTC_U8)0x00
#define USB_CLASS_REQUEST        	(USTC_U8)0x20
#define USB_VENDOR_REQUEST       	(USTC_U8)0x40

#define USB_REQUEST_MASK         	(USTC_U8)0xFF

#define DEVICE_ADDRESS_MASK      	0x7F

#define DEVSTS_SELFPOWERED       	0x01
#define DEVSTS_REMOTEWAKEUP     	0x02

#define ENDPSTS_HALT            			 0x01


//deric 
#define USB_CLASS_CDC_CLASS_DEVICE			0x02

//
//USB protocol function pointer arrays,stardard device request
//
typedef enum _StandardDeviceRequest_TYPE{
	P_FUNC_CHAP9_GETSTATUS,
	P_FUNC_CHAP9_CLEARFEATURE,
	P_FUNC_USB_STALL_EP0,
	P_FUNC_CHAP9_SETFEATURE,
	P_FUNC_USB_STALL_EP0_1,
	P_FUNC_CHAP9_SETADDRESS,
	P_FUNC_CHAP9_GETDESCRIPTOR,
	P_FUNC_USB_STALL_EP0_2,
	P_FUNC_CHAP9_GETCONFIGURATION,
	P_FUNC_CHAP9_SETCONFIGURATION,
	P_FUNC_CHAP9_GETINTERFACE,
	P_FUNC_CHAP9_SETINTERFACE
}StandardDeviceRequest_TYPE;


typedef enum _ClassDeviceRequest_TYPE
{
	P_FUNC_BULK_GETMAXLUN =0xfe
}ClassDeviceRequest_TYPE;


/*descriptor structure defination*/

typedef PACKED struct {
    USTC_U8		bLength	;
    USTC_U8 		bDescriptorType	;
    USTC_U16 		bcdUSB;
    USTC_U8		bDeviceClass;
    USTC_U8 		bDeviceSubClass	;
    USTC_U8 		bDeviceProtocol	;
    USTC_U8 		bMaxPacketSize0;
    USTC_U16 		idVendor;
    USTC_U16 		idProduct;
    USTC_U16 		bcdDevice;
    USTC_U8 		iManufacturer;
    USTC_U8 		iProduct	;
    USTC_U8 		iSerialNumber;
    USTC_U8 		bNumConfigurations;
} USB_DEVICE_DESCRIPTOR_T , *PUSB_DEVICE_DESCRIPTOR_T;

typedef PACKED struct  {
    USTC_U8 		bLength;
    USTC_U8 		bDescriptorType;
    USTC_U16 		wTotalLength;
    USTC_U8 		bNumInterfaces;
    USTC_U8 		bConfigurationValue;
    USTC_U8 		iConfiguration;
    USTC_U8 		bmAttributes;
    USTC_U8 		MaxPower;
}  USB_CONFIGURATION_DESCRIPTOR_T, *PUSB_CONFIGURATION_DESCRIPTOR_T;

typedef PACKED struct  {
    USTC_U8 		bLength;
    USTC_U8 		bDescriptorType;
    USTC_U8 		bInterfaceNumber;
    USTC_U8 		bAlternateSetting;
    USTC_U8 		bNumEndpoints;
    USTC_U8 		bInterfaceClass;
    USTC_U8 		bInterfaceSubClass;
    USTC_U8 		bInterfaceProtocol;
    USTC_U8 		iInterface;
}  USB_INTERFACE_DESCRIPTOR_T, *PUSB_INTERFACE_DESCRIPTOR_T;

typedef PACKED struct  {
    USTC_U8 		bLength;
    USTC_U8 		bDescriptorType;
    USTC_U8 		bEndpointAddress;
    USTC_U8 		bmAttributes;
    USTC_U16 		wMaxPacketSize;
    USTC_U8 		bInterval;
} USB_ENDPOINT_DESCRIPTOR_T, *PUSB_ENDPOINT_DESCRIPTOR_T;


typedef PACKED  struct  {
	USTC_U8	bLength;
    USTC_U8 	bDescriptorType;
    USTC_U8 	bString[1];
}USB_STRING_DESCRIPTOR_T, *PUSB_STRING_DESCRIPTOR_T;

typedef  PACKED struct {
	USTC_U8  	bLength;
	USTC_U8  bDescriptorType;
	USTC_U16 ulanguageID;
} USB_STRING_LANGUAGE_DESCRIPTOR_T,*PUSB_STRING_LANGUAGE_DESCRIPTOR_T;

typedef PACKED struct {
	USTC_U8  bLength;
	USTC_U8  bDescriptorType;
	USTC_U8  Interface[22];
} USB_STRING_INTERFACE_DESCRIPTOR_T,*PUSB_STRING_INTERFACE_DESCRIPTOR_T;

typedef PACKED struct {
	USTC_U8  bLength;
	USTC_U8  bDescriptorType;
	USTC_U8  Configuration[16];
}USB_STRING_CONFIGURATION_DESCRIPTOR_T,*PUSB_STRING_CONFIGURATION_DESCRIPTOR_T;

typedef PACKED struct {
	USTC_U8  bLength;
	USTC_U8  bDescriptorType;
	USTC_U8  SerialNum[24];
}  USB_STRING_SERIALNUMBER_DESCRIPTOR_T,*PUSB_STRING_SERIALNUMBER_DESCRIPTOR_T;

typedef PACKED struct  {
	USTC_U8  bLength;
	USTC_U8  bDescriptorType;
	USTC_U8  Product[30];
} USB_STRING_PRODUCT_DESCRIPTOR_T,*PUSB_STRING_PRODUCT_DESCRIPTOR_T;

typedef PACKED struct  {
	USTC_U8  bLength;
	USTC_U8  bDescriptorType;
	USTC_U8  Manufacturer[24];
}USB_STRING_MANUFACTURER_DESCRIPTOR_T,*PUSB_STRING_MANUFACTURER_DESCRIPTOR_T;

void chap9_control_transmit(unsigned char *buff,unsigned short len);

/*APIs*/
extern void chap9_get_descriptor(void);
extern void chap9_set_configuration(void);

extern void chap9_get_status(void);
extern void chap9_get_configuration(void);
extern void chap9_set_interface(void);
extern void chap9_get_interface(void);
extern void chap9_clear_feature(void);
extern void chap9_set_feature(void);
extern void chap9_set_address(void);

extern void chap9_class_get_max_lun(void);
extern void chap9_vendor_cmd_process(void);

#endif
